package com.axter.app.process;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import xiaofei.library.hermeseventbus.HermesEventBus;

/**
 * Created by apple on 2017/6/29.
 */

public class A extends AppCompatActivity implements View.OnClickListener {
    TextView tv_info;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        Button go = (Button) findViewById(R.id.go);
        Button get = (Button) findViewById(R.id.get);
        tv_info = (TextView) findViewById(R.id.tv_info);
        go.setOnClickListener(this);
        get.setOnClickListener(this);

        StringBuilder sb = new StringBuilder();
        sb.append("当前线程id:" + Thread.currentThread().getId());
        sb.append("\n");
        sb.append("当前进程Pid:" + android.os.Process.myPid());
        sb.append("\n");
        sb.append("当前进程名称:" + getCurProcessName(this));
        sb.append("\n");
        sb.append("当前申请的总内存:" + Runtime.getRuntime().totalMemory() / 1024 / 1024 + "M");
        sb.append("\n");
        sb.append("单个进程分配的内存上限:" + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M");
        sb.append("\n");
        tv_info.setText(sb.toString());




        HermesEventBus.getDefault().register(this);
        Intent intent = new Intent();
        intent.setPackage("com.axter.app.process");
        intent.setAction("com.axter.app.process.IMyService");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myService = IMyService.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    IMyService myService;

    @Override
    public void onClick(View var1) {
        switch (var1.getId()) {
            case R.id.go:
                startActivity(new Intent(this, B.class));
                break;
            case R.id.get:
                try {
                    if(myService == null){
                        return;
                    }
                    System.out.println(myService.getValue());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {

                return appProcess.processName;
            }
        }
        return null;
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(this.getClass().getName(), "onStop");


        tv_info.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        },2000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(this.getClass().getName(), "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(this.getClass().getName(), "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(this.getClass().getName(), "onDestroy");
        System.exit(0);

//        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
//        manager.killBackgroundProcesses(getPackageName());

//        android.os.Process.killProcess(android.os.Process.myPid());

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showText(DD text) {
        System.out.println(text);
    }
}
